/*
 * Copyright ©2015-2021 Jaemon. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.jaemon.spring.adorner.core;

import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.FieldDoc;
import com.sun.javadoc.MethodDoc;
import com.sun.javadoc.RootDoc;
import io.gitee.jaemon.spring.adorner.util.StringUtils;

import java.lang.reflect.Modifier;

/**
 * 自定义Doclet
 *
 * @author Jaemon
 * @since 1.0
 */
public class AdornerDoclet {
    static RootDoc root;

    public AdornerDoclet() {
    }

    /**
     * 启动方法
     *
     * @param root
     *      doc信息对象
     * @return
     *      true | false
     */
    public static boolean start(final RootDoc root) {
        AdornerDoclet.root = root;
        return true;
    }


    /**
     * 扫描文档信息
     */
    public static void scanner() {
        ClassDoc[] classes = AdornerDoclet.root.classes();
        for (int i = 0; i < classes.length; ++i) {
            ClassDoc clazz = classes[i];
            if (clazz.isEnum() || clazz.isAnnotationType()) {
                continue;
            }
            String clazzComment = clazz.commentText().trim();
            if (!StringUtils.isEmpty(clazzComment)) {
                DocContainer.CONTAINER.classPut(clazz.qualifiedName(), notesConvert(clazzComment));
            }

            for (MethodDoc method : classes[i].methods()) {
                int methodModifier = method.modifierSpecifier();
                String commentText = method.commentText().trim();
                if (
                        !StringUtils.isEmpty(commentText) &&
                                Modifier.isPublic(methodModifier) &&
                                !Modifier.isStatic(methodModifier)
                ) {
                    DocContainer.CONTAINER.methodPut(method.qualifiedName(), notesConvert(commentText));
                }
            }

            for (FieldDoc field : classes[i].fields()) {
                int fieldModifier = field.modifierSpecifier();
                String commentText = field.commentText().trim();
                boolean flag = !StringUtils.isEmpty(commentText) &&
                        !(
                                Modifier.isStatic(fieldModifier) ||
                                        Modifier.isFinal(fieldModifier)
                        );
                if (flag){
                    DocContainer.CONTAINER.fieldPut(field.qualifiedName(), notesConvert(commentText));
                }
            }
        }
    }

    /**
     * 注释转换
     *
     * @param notes
     *          注释内容
     * @return
     *          转换后注释内容
     */
    private static String notesConvert(String notes) {
        if (StringUtils.isEmpty(notes)) {
            return "";
        }
        String[] split = notes.split("\r\n|\n");
        if (split == null) {
            return null;
        }

        String ns = split[0];
        return ns.length() > 64 ? ns.substring(0, 64) + "……" : ns;


    }
}